//
//  MarketTabViewController.swift
//  Exchange
//
//  Created by rui on 2018/5/7.
//  Copyright © 2018年 common. All rights reserved.
//

import UIKit

class MarketTabViewController: TabbarPageController {

    private let kMarketTableViewCell = "MarketTableViewCell"
    
    private let rowHeight: CGFloat = 60
    private let headerHeight: CGFloat = 35
    
    var models: [MarketCoinModel] {
        let titles = APPTransactionPair.default.archiveTitleArray()
        if let index = self.index, titles.count > index {
            let title = titles[index]
            let models = APPTransactionPair.default.pairDic[title]
            return models!
        }
        return []
    }
    
    lazy var emptyView: EmptyView = {
        let empty = EmptyView()
        empty.message = kText("暂无相关数据")
        return empty
    }()
    
    
    var canScroll = false
    
    lazy var tableView: UITableView = {
        let table = UITableView(frame: self.bounds, style: .plain)
        let header = MarketTableHeaderView(frame: CGRect(x: 0, y: 0, width: kSCREEN_WIDTH, height: headerHeight))
        table.tableHeaderView = header
        table.autoresizingMask = [.flexibleWidth,.flexibleHeight]
        table.delegate = self
        table.dataSource = self
        table.themeBackground = ThemeManager.ThemeColorKeys.background
        table.register(UINib(nibName: kMarketTableViewCell, bundle: nil), forCellReuseIdentifier: kMarketTableViewCell)
        table.tableFooterView = UIView()
        table.separatorStyle = .none
        table.showsHorizontalScrollIndicator = false
        table.showsVerticalScrollIndicator = false
        return table
    }()

    override func viewDidLoad() {
        self.addSubview(tableView)
    }
    
    override func viewWillAppear() {
        SocketManager.shared.delegate = self
        APPTransactionPair.default.delegate = self
        let titles = APPTransactionPair.default.archiveTitleArray()
        APPTransactionPair.default.subscribe(key: titles[self.index ?? 0])
        self.reload()
    }

    override func viewWillDisappear() {
        let titles = APPTransactionPair.default.archiveTitleArray()
        APPTransactionPair.default.cancelSubscribe(key: titles[self.index ?? 0])
    }
    
    func reload() {
        emptyView.removeFromSuperview()
        if models.count <= 0 {
            emptyView.frame = self.tableView.bounds
            emptyView.autoresizingMask = [.flexibleWidth,.flexibleHeight]
            self.tableView.addSubview(emptyView)
        }
        self.tableView.reloadData()
    }
}

extension MarketTabViewController: UITableViewDelegate,UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return models.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kMarketTableViewCell, for: indexPath) as! MarketTableViewCell
        let model = models[indexPath.row]
        cell.model = model
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return rowHeight
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: false)
        let model = models[indexPath.row]
        let vc = KlineViewController()
        vc.viewModel.coinModel = model
        self.viewController()?.navigationController?.pushViewController(vc, animated: true)
    }
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if canScroll == false {
            scrollView.contentOffset = CGPoint.zero
        }
        if scrollView.contentOffset.y <= 0 {
            canScroll = false
            scrollView.contentOffset = CGPoint.zero
            NotificationCenter.default.post(name: NSNotification.Name.Exchange.HomePageScroll, object: nil)
        }
    }

}


extension MarketTabViewController: SocketManagerDelegate,TransactionPairDelegate {
    func socket(didReadTick data: [String : Any]) {
        APPTransactionPair.default.updateParameters(data:data)
        self.reload()
    }
}




